// Copyright (c) 2009 Alistair Jones
// This program is made available under the terms of the MIT License.
package org.hypirinha.html.codegen;

import junit.framework.Assert;
import junit.framework.AssertionFailedError;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class MemberExpectations {
    private Member member;

    public MemberExpectations(Member member) {
        this.member = member;
    }

    public void shouldRenderTo(Line... expectedLines) {
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        member.printTo(new PrintStream(arrayOutputStream));
        String[] actualLines = new String(arrayOutputStream.toByteArray()).split("\n");

        try {
            Assert.assertEquals(expectedLines.length, actualLines.length);
            for (int i = 0; i < expectedLines.length; i++) {
                Assert.assertEquals(expectedLines[i].toString(), actualLines[i]);
            }
        } catch (AssertionFailedError e) {
            System.out.println("actualLines = \n" + arrayOutputStream.toString());
            throw e;
        }
    }

    static Line line(String value) {
        return new Line(value);
    }

    static MemberExpectations member(Member member) {
        return new MemberExpectations(member);
    }

    public static class Line {
        private String value;

        public Line(String value) {
            this.value = value;
        }

        public String toString() {
            return value;
        }
    }
}
